<?php
namespace app\index\controller;

use app\common\model\Enroll as EnrollModel;
use app\common\model\Salary as SalaryModel;
use app\common\model\Company as CompanyModel;
use think\Loader;
use think\Request;
use app\common\model\Withdraw as WithdrawModel;
use app\common\model\Order    as OrderModel;
use think\Db;

/**
 * 商家的接口需要判断商家是否登录
 * Class Store
 * @package app\index\controller
 */
class Store extends StoreBase {

    /**
     * 我的提成列表
     */
    public function money(){
        $salary = SalaryModel::where(['company_id'=>$this->company['id']])->order('add_time desc')->paginate(30);
        foreach($salary as $key => &$val ){
            $val['add_time'] = date('Y-m-d',$val['add_time']);
        }
        $this->success("获取成功",'',$salary);
    }


    /**
     * 获取公司信息
     */
    public function companyInfo(){
        $company = CompanyModel::where(['id'=>$this->company['id']])->find();
        $this->success('获取成功','',$company);
    }

    /**
     * 我的用户  这种关系 是一种弱关系 也就是说 下一次报名 也有可能报名在别人那里
     * 根据级别 有不同的查找
     * 工厂和世博园是没有我的用户的
     */
    public function myCustomer(){

        $customers = [];
        if($this->company['type'] == 0){
            $customers = EnrollModel::where(['company_one_id'=>$this->company['id']])->distinct(true)->field('mobile,real_name')->select();
        }elseif($this->company['type'] == 1) {
            $customers = EnrollModel::where(['company_two_id' => $this->company['id']])->distinct(true)->field('mobile,real_name')->select();
        }

        //根据字母给用户排序 这样的话岂不是要一次性弄出来
        $this->success('获取成功','',$customers);

    }



    /**
     * 申请提现
     * 只有在一次提现申请完成 或者失败才允许下一次提现
     * @param Request $request
     * @throws
     */
    public function applyWithdraw(Request $request){
        $post = $request->post();
        $validate = Loader::validate('Withdraw');
        if(!$validate ->check($post)){
            $this->error($validate->getError());
        }

        //判断是否申请多次
        $exists = WithdrawModel::where(['company_id'=>$this->company['id'],'state'=>0])->find();
        if($exists){
            $this->error("你已经提交过一次，请等待处理结束");
        }

        //判断是否超过数额
        $company = CompanyModel::where(['id'=>$this->company['id']])->find();
        if($company['balance'] < $post['money']){
            $this->error("提现金额超过余额");
        }
        if($post['money'] <= 0){
            $this->error("金额不对");
        }

        $post['company_id'] = $this->company['id'];
        $post['add_time']  = time();


        $res = WithdrawModel::create($post);
        if($res){
            $this->success("已经提交申请");
        }

        $this->error("申请失败");
        
    }


    /**
     * 城市运营商的B端列表
     * @throws
     */
    public function myChain(){
        $companies = CompanyModel::where(['belong_to'=>$this->company['id']])->paginate(30);
        $this->success('获取成功','',$companies);
    }


    /**
     * 工厂和世博园的订单
     */
    public function order(){
        //工厂

        if($this->company['type'] == 2){
            $where = ['company_three_id'=>$this->company['id']];
        }elseif($this->company['type'] == 3){//世博园
            $where = ['company_four_id'=>$this->company['id']];
        }else{
            $this->error("获取失败");
        }

        $order_list = OrderModel::where($where)->order('add_time desc')->paginate(30);
        $order_list = json_encode($order_list,true);
        $order_list = json_decode($order_list,true);
        $order_list = $order_list['data'];



        //查找产品图片
        $order_ids = [];
        $list = [];
        foreach($order_list as $key => $val ){
            array_push($order_ids,$val['id']);
            $list[$val['id']] = $val;
            $list[$val['id']]['picture_list'] = [];
        }

        $order_ids = array_unique($order_ids);

        //SELECT * FROM order_product AS op INNER JOIN product AS p ON  op.`product_id` = p.id WHERE op.order_id IN (1,2);
        $picture_list = Db::table("order_product")->alias("op")->join("product p",'op.product_id = p.id')->where('op.order_id','in',$order_ids)->field('order_id,p.id as product_id,cover')->select();

        foreach( $picture_list as $key => $val ){
            array_push($list[$val['order_id']]['picture_list'],$val['cover']);
        }


        $this->success('获取成功','',$list);
    }


}